package com.example.demo.util;

import java.io.Serializable;

public class Singleton implements Serializable {

    private Singleton() {
        synchronized (Singleton.class) {
            if (instance != null) {
                throw new RuntimeException();
            }
        }
    }

    private static final Singleton instance = new Singleton();

    /**
     * 获取单利对象
     *
     * @return
     */
    public static Singleton getInstance() {
        return instance;
    }

    /**
     * 防止反序列化时创建对象，方法名字是默认实现是 readResolve
     * 这种方式只针对于 ObjectOutputStream/ObjectInputStream 的处理方式
     *
     * @return
     */
    private Object readResolve() {
        return instance;
    }

}
